<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button style="float:right" type="primary" @click="getDataList()" size="small">
          查询搜索
        </el-button>
        <el-button style="float:right;margin-right: 15px" @click="handleResetSearch()" size="small">
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px" @keyup.enter.native="getDataList()">
          <el-form-item label="输入搜索：">
            <el-input v-model="listQuery.id" placeholder="请输入id" clearable></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button size="mini" class="btn-add" type="danger" style="margin-left: 20px" @click="deleteHandle()" :disabled="dataListSelections.length <= 0" >批量删除</el-button>
      <el-button size="mini" class="btn-add" type="primary" @click="addOrUpdateHandle()">新增</el-button>
    </el-card>
    <div class="table-container">
      <el-table :data="list" border v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;">
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
          <el-table-column prop="title" label="标题" header-align="center" align="center"></el-table-column>
          <el-table-column prop="picUrl" label="展示图片" header-align="center" align="center">
            <template slot-scope="{row}">
              <el-image
                :src=" baseApi + row.picUrl"
                :preview-src-list="[baseApi + row.picUrl]"
                fit="contain"
                lazy
                class="el-avatar"
              >
                <div slot="error">
                  <i class="el-icon-document" />
                </div>
              </el-image>
            </template>
          </el-table-column>
          <el-table-column prop="position" label="展示位置" header-align="center" align="center">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.position === 1" size="small">首页</el-tag>
              <el-tag v-if="scope.row.position === 2" size="small">内页</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="startTime" label="开始时间" header-align="center" align="center"></el-table-column>
          <el-table-column prop="endTime" label="结束时间" header-align="center" align="center"></el-table-column>
          <el-table-column prop="enabled" label="是否启用" header-align="center" align="center">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.enabled === 0" size="small" type="danger">禁用</el-tag>
              <el-tag v-if="scope.row.enabled === 1" size="small">正常</el-tag>
            </template>
          </el-table-column>
        <el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
            <el-button size="mini" type="text" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页控件 -->
    <div class="pagination-container">
      <el-pagination
        background
        class="table-pagination"
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :total="total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import { fetchBannerList, deleteBanner } from '@/api/knowledge/banner';
import AddOrUpdate from './banner-add-or-update';
const baseUrl = process.env.VUE_APP_FILE_URL === '/' ? '' : process.env.VUE_APP_FILE_URL;

const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
};

export default {
  data () {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: [],
      total: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      baseApi: baseUrl,
    }
  },
  components: {
    AddOrUpdate
  },
  created() {
    this.getDataList()
  },
  methods: {
    // 重置查询表单
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      fetchBannerList(this.listQuery).then(response => {
        this.dataListLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.listQuery.pageNum = 1
      this.listQuery.pageSize = val
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.listQuery.pageNum = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
       this.$confirm(`确定对[id=${ids.join(',')}]进行[删除]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteBanner(ids).then(res => {
          if (res && res.code === 200) {
            
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1000
              })
            
            this.getDataList()
          } else {
             this.$message.error(res.msg) 
          }
        })
      }).catch(() => {})
    },
  }
}
</script>
